package com.jar.exercise.io.随机读写;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;

public class RafDemo {
	public static void main(String[] args) throws IOException {
		write();
		//read();
		//write_2();
//        read2();
	}

	public static void write() throws IOException {
		File w = new File("D:\\Study\\IDEA\\vue\\Demo\\src\\io\\随机读写\\access.txt");
		RandomAccessFile raf = new RandomAccessFile(w, "rw");
		raf.writeBytes("jim");
		raf.writeInt(24);
		raf.writeBytes("tom");
		raf.writeInt(23);
		raf.writeBytes("john");
		raf.writeInt(22);
		raf.close();

	}

	public static void write2() throws IOException {
		File w = new File("D:\\Study\\IDEA\\vue\\Demo\\src\\io\\随机读写\\access.txt");
		RandomAccessFile raf = new RandomAccessFile(w, "rw");
		String name = "张胜豪";
		byte[] bytes = name.getBytes();
		raf.write(bytes);
		raf.close();
	}

	public static void read2() throws IOException {
		File w = new File("d:/w.txt");
		RandomAccessFile raf = new RandomAccessFile(w, "r");
		byte[] b = new byte[6];
		raf.read(b);
		String s = new String(b);
		System.out.println(s);
	}


	public static void read() throws IOException {
		File w = new File("d:/w.txt");
		RandomAccessFile raf = new RandomAccessFile(w, "r");
		raf.skipBytes(14);
		byte[] b = new byte[4];
		raf.read(b);
		String n3 = new String(b);
		int a3 = raf.readInt();
		System.out.println("�������ˣ�" + n3 + "," + a3 + "��");
		raf.seek(0);
		b = new byte[3];
		raf.read(b);
		String n1 = new String(b);
		int a1 = raf.readInt();
		System.out.println("��һ���ˣ�" + n1 + "," + a1 + "��");
		raf.close();
		System.out.println("��ȡ�ɹ�");
	}
}

